using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class InventoryAdjustmentDTO : INotifyPropertyChanged
    {
        private String _Description;
        private ObservableCollection<InventoryAdjustmentDetailDTO> _InventoryAdjustmentDetails;
        private int _No;
        private String _NoAdj;
        private DateTime? _TglAdjustment;
        private string _Type;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;
        private Status _Status;


        public InventoryAdjustmentDTO()
        {
            TglAdjustment = DateTime.Now.Date;
        }

        [DataMember]
        public String NoAdj
        {
            get { return _NoAdj; }
            set
            {
                _NoAdj = value;
                RaisePropertyChanged("NoAdj");
            }
        }

        [DataMember]
        public DateTime? TglAdjustment
        {
            get { return _TglAdjustment; }
            set
            {
                _TglAdjustment = value;
                RaisePropertyChanged("TglAdjustment");
            }
        }

        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public ObservableCollection<InventoryAdjustmentDetailDTO> InventoryAdjustmentDetails
        {
            get { return _InventoryAdjustmentDetails; }
            set
            {
                if (value == InventoryAdjustmentDetails) return;
                _InventoryAdjustmentDetails = value;
                RaisePropertyChanged("InventoryAdjustmentDetails");
                RefreshNumber();
            }
        }


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }

        [DataMember]
        public string Type
        {
            get { return _Type; }
            set
            {
                _Type = value;
                RaisePropertyChanged("Type");
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        [DataMember]
        public Status Status
        {
            get { return _Status; }

            set
            {
                _Status = value;
                RaisePropertyChanged("Status");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RefreshNumber()
        {
            InventoryAdjustmentDetails.CollectionChanged += InventoryAdjustmentDetails_CollectionChanged;
            int number = 0;
            foreach (InventoryAdjustmentDetailDTO DetailDto in InventoryAdjustmentDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        private void InventoryAdjustmentDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (InventoryAdjustmentDetailDTO DetailDto in InventoryAdjustmentDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}